﻿using System.Web.Mvc;
using ComeSeeThis_MVC2.Models;
//using ComeSeeThis_MVC2.

namespace ComeSeeThis_MVC2.Controllers
{
    public class MotorController : Controller
    {
        readonly MotorRepository _motorRepository = new MotorRepository();

        #region Get Methods

        public ActionResult Index(int page = 0, int pageSize = 10)
        {
            //const int pageSize = 10;
            var motors = _motorRepository.GetAllMotors();

            var paginatedMotors = new PaginatedList<Motor>(motors, page, pageSize);

            return View(paginatedMotors);
        }

        public ActionResult Details(int id)
        {
            var motor = _motorRepository.GetMotor(id);

            if (motor == null) return View("MotorNotFound");

            return View(motor);
        }

        #endregion

        #region Edit Methods

        [Authorize]
        public ActionResult Edit(int id)
        {
            var motor = _motorRepository.GetMotor(id);
            if (motor == null) return View("MotorNotFound");

            // Is the user allowed to edit this motor?
            return Security.CanEditMotor(motor.MotorId, User.Identity.Name) ? View(new MotorFormView(motor)) : View("AccessDenied");
        }

        [HttpPost, Authorize] //(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            var motor = _motorRepository.GetMotor(id);

            if (TryUpdateModel(motor, "Motor"))
            {
                _motorRepository.Save();
                return RedirectToAction("Index");
            }
            return View(new MotorFormView(motor));
        }

        #endregion

        #region Create Methods

        [Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Create()
        {
            var motor = new Motor();

            return View(new MotorFormView(motor));
        }

        [HttpPost, Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Create(Motor motor)
        {
            if (ModelState.IsValid)
            {
                motor.SubmittedBy = User.Identity.Name;
                _motorRepository.Add(motor);
                _motorRepository.Save();

                if (motor.Approved) RedirectToAction("Index");
                return RedirectToAction("Submitted", new { id = motor.MotorId });

                //return RedirectToAction("Create");
            }
            return View(new MotorFormView(motor));
        }
        public ActionResult Submitted(int id)
        {
            var motor = _motorRepository.GetMotor(id);

            return View(motor);
        }

        #endregion

        #region Delete Methods

        [Authorize]//(Roles = "SiteAdmin,GearingAdmin")]
        public ActionResult Delete(int id)
        {
            var motor = _motorRepository.GetMotor(id);

            return motor == null ? View("MotorNotFound") : View(motor);
        }

        [HttpPost, Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Delete(int id, string confirmButton)
        {
            var motor = _motorRepository.GetMotor(id);

            if (motor == null) return View("MotorNotFound");

            _motorRepository.Delete(motor);
            _motorRepository.Save();

            return RedirectToAction("Index");
        }

        #endregion
    }
}
